﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;
using System.Globalization;

namespace Tireguit.Nmon.Parser.Statisitic
{
    public class PAGEElement:NmonStatElement
    {
         protected new ILog _log = LogManager.GetLogger(typeof(PAGEElement));
        protected IDataParameter _zzzz = null;
        protected IDataParameter _faults = null;
        protected IDataParameter _pgin = null;
        protected IDataParameter _pgout = null;
        protected IDataParameter _pgsin = null;
        protected IDataParameter _pgsout = null;
        protected IDataParameter _reclaims = null;
        protected IDataParameter _scans = null;
        protected IDataParameter _cycles = null;
        public string Zzzz { get; protected set; }
        public float Faults { get; protected set; }
        public float PgIn { get; protected set; }
        public float PgOut { get; protected set; }
        public float PgsIn { get; protected set; }
        public float PgsOut { get; protected set; }
        public float Reclaims { get; protected set; }
        public float Scans { get; protected set; }
        public float Cycles { get; protected set; }
        public PAGEElement(IDbConnection connection)
        {
            Balise = BaliseType.PAGE;
            ListCommand = new List<System.Data.IDbCommand>();
            Connection = connection;
            Linestring = string.Empty;
            _insertreq = "INSERT INTO " + NmonTables.TablesDictionnary.Instance["PAGE"] + " ([zzzz],[faults],[pgin],[pgout],[pgsin],[pgsout],[reclaims],[scans],[cycles]) VALUES (@zzzz,@faults,@pgin,@pgout,@pgsin,@pgsout,@reclaims,@scans,@cycles)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _zzzz = Command.CreateParameter();
            _zzzz.ParameterName = "@zzzz";
            _zzzz.DbType = DbType.String;
            _faults = Command.CreateParameter();
            _faults.ParameterName = "@faults";
            _faults.DbType = DbType.Decimal;
            _pgin = Command.CreateParameter();
            _pgin.ParameterName = "@pgin";
            _pgin.DbType = DbType.Decimal;
            _pgout = Command.CreateParameter();
            _pgout.ParameterName = "@pgout";
            _pgout.DbType = DbType.Decimal;
            _pgsin = Command.CreateParameter();
            _pgsin.ParameterName = "@pgsin";
            _pgsin.DbType = DbType.Decimal;
            _pgsout = Command.CreateParameter();
            _pgsout.ParameterName = "@pgsout";
            _pgsout.DbType = DbType.Decimal;
            _reclaims = Command.CreateParameter();
            _reclaims.ParameterName = "@reclaims";
            _reclaims.DbType = DbType.Decimal;
            _scans = Command.CreateParameter();
            _scans.ParameterName = "@scans";
            _scans.DbType = DbType.Decimal;
            _cycles = Command.CreateParameter();
            _cycles.ParameterName = "@cycles";
            _cycles.DbType = DbType.Decimal;
        }
        public PAGEElement() : this(null) { }
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (Command == null)
                {
                    Command = Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);
                }
                Command.Parameters.Clear();
                _zzzz.Value = Zzzz;
                _faults.Value = Faults;
                _pgin.Value = PgIn;
                _pgout.Value = PgOut;
                _pgsin.Value = PgsIn;
                _pgsout.Value = PgsOut;
                _reclaims.Value = Reclaims;
                _scans.Value = Scans;
                _cycles.Value = Cycles;
                Command.Parameters.Add(_zzzz);
                Command.Parameters.Add(_faults);
                Command.Parameters.Add(_pgin);
                Command.Parameters.Add(_pgout);
                Command.Parameters.Add(_pgsin);
                Command.Parameters.Add(_pgsout);
                Command.Parameters.Add(_reclaims);
                Command.Parameters.Add(_scans);
                Command.Parameters.Add(_cycles);
                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (!elements[0].StartsWith("PAGE"))
                {
                    _log.Error("Not PAGE Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                if (elements[1].StartsWith("Pag"))
                {
                    //Skip information header 
                    return false;
                }
                Zzzz = elements[1];
                float res = -1;
                if (float.TryParse(elements[2], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Faults = res;

                }
                if (float.TryParse(elements[3], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    PgIn = res;

                }
                if (float.TryParse(elements[4], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    PgOut = res;

                }
                if (float.TryParse(elements[5], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    PgsIn = res;

                }
                if (float.TryParse(elements[6], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    PgsOut = res;

                }
                if (float.TryParse(elements[7], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Reclaims = res;

                }
                if (float.TryParse(elements[8], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Scans = res;

                }
                if (float.TryParse(elements[9], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Cycles = res;

                }


                return true;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
